/**
 * Created by aa on 2016/4/21.
 */
window.onload=function() {
    $("#loading").css('display','none')
    $(".swiper-container").css('display','block');

    var instance = new hktouch();
    instance.init();
    $(".iphoneDownload").on("click tap", function () {
        if(browser.versions.android){
            alert('安卓机型请选择安卓下载！');
            return;
        }
        if (browser.versions.weiXin) {
            location.href = "downLoadHelp.html";
        }
        else {
            location.href = "https://itunes.apple.com/cn/app/id1099068949?mt=8";
        }
    });

    $(".androidDownload").on("click tap", function () {
    /*    if(!browser.versions.android){
            alert('IOS机型请选择IOS下载！');
            return;
        }*/
        if (browser.versions.weiXin) {
            location.href = "downLoadHelp.html";
        }
        else {
            $.ajax({
                url:UrlInformation.apipath+'/app/parmCtrl/getPvUrlParm',
                type: "GET",
                contentType: 'application/json',
                success:function(data){
                    var result=data.result;
                    location.href = result;
                },
                error:function(error){
                    alert('获取数据出错，请稍后再试！');
                }
            });

        }
    });

    if (browser.versions.mobile) {
        $(".iphoneDownload,.androidDownload").off('click');
    }
    else {
        $(".iphoneDownload,.androidDownload").off('tap');
    }
};



function hktouch(){
    this.init=function() {
        var self=this;
        var mySwiper = new Swiper('.swiper-container', {
            pagination: '.pagination',
            paginationClickable: true,
            mode: 'vertical',
            onSlideChangeEnd: function (swiper) {
                if(swiper.activeIndex>swiper.previousIndex){
                    self.animationEvent(swiper.activeIndex);
                }
            }
        });
        this.animationEvent(0);
    }


    //显示动画
    this.animationEvent=function(pageNum){
       // var self=this;
        switch (pageNum){
            case 0:
                this.firstEvent();
                break;
            case 1:
                this.secondEvent();
                break;
            case 2:
                this.thirdEvent();
                break;
            case 3:
                this.forthEvent();
                break;
            case 4:
                this.fifthEvent();
                break;
        }
    }


    //动画不直接改变属性，动画结束后所有的属性不会发生改变，故一开始隐藏元素，动画开始后显示出来
    //动画jquery对象，动画类型，周期时间（微秒），轨迹函数，延迟时间（微秒）,回调函数
    this.commonAnimate=function(adminateObj,animateType,duration,timingFunction,delay,callBack){
        if(delay>0){
            window.setTimeout(function(){
                $(adminateObj).css('visibility','visible');
            },delay);
        }
        else{
            $(adminateObj).css('visibility','visible');
        }
        animationOperate=animateType+' '+duration+'ms '+timingFunction+' '+delay+'ms'
        $(adminateObj).css('animation',animationOperate);
        $(adminateObj).bind('animationend webkitAnimationEnd', function() {
            if(callBack) {
                callBack();
            }
        });
    };

    this.firstEvent=function(){
      /*  $(".swiper-wrapper").children(".swiper-slide").eq(0).children(".mainImg").animate({
            bottom: '-5vh'
        },'slow',function(){
            $(".swiper-wrapper").children(".swiper-slide").eq(0).children(".message").animate({
                right:'-5vw'
            });
            $(".swiper-wrapper").children(".swiper-slide").eq(0).children(".message").animate({
                right:'0'
            },'fast');
        });
        $(".swiper-wrapper").children(".swiper-slide").eq(0).children(".mainImg").animate({
            bottom: '0'},'fast');*/

        var self=this;
        var firstAnimateObj=$("#messageG");
        //文字
        self.commonAnimate(firstAnimateObj,'slideFromRight',500,'linear',0,function(){
            //手机
            var animateObj=$(".swiper-wrapper").children(".swiper-slide").eq(0).children(".mainImg");
            self.commonAnimate(animateObj,'firstMainImg',500,'linear',0,function() {
                //文字
                self.commonAnimate(firstAnimateObj, 'slideToLeft', 500, 'linear', 100, function () {
                    firstAnimateObj.css('display','none');
                    //按钮
                    var nextAnimateObj = $(".swiper-wrapper").children(".swiper-slide").eq(0).children(".message");
                    self.commonAnimate(nextAnimateObj, 'slideFromRight', 500, 'linear', 0);
                });
            });
        });

    };
    this.secondEvent=function(){
        var self=this;
        var animateObj=$(".swiper-wrapper").children(".swiper-slide").eq(1).children(".mainImg");
        this.commonAnimate(animateObj,'firstMessage',500,'linear',0,function(){
            $(".movingText:nth-child(3)").css("opacity","100")
            $(".movingText:nth-child(4)").css("opacity","100")
            $(".movingText:nth-child(5)").css("opacity","100")
            var nextAnimateObj=$(".swiper-wrapper").children(".swiper-slide").eq(1).children(".message");
            self.commonAnimate(nextAnimateObj,'visibleTransform',500,'linear',0,function(){
                $(".movingText:nth-child(1)").css("opacity","100")
                $(".movingText:nth-child(2)").css("opacity","100")
               /* $(".movingText:nth-child(4)").css("opacity","100")*/
            });
        });
    }

    this.thirdEvent=function(){
        var self=this;
        var animateObj=$(".swiper-wrapper").children(".swiper-slide").eq(2).children(".mainImg");
        this.commonAnimate(animateObj,'firstMainImg',500,'linear',0,function(){
            var nextAnimateObj=$(".swiper-wrapper").children(".swiper-slide").eq(2).children(".message");
            self.commonAnimate(nextAnimateObj,'rotateyTransform',500,'linear',1);
          /*  $('.profit').css('background-color','#e97454');
           $('.profit').css('marginLeft','-12em');
            $('.profit').css('font-size','25px');
            $('.profit').css('top','8%');
           $('.profit').css('height','9em');
            $('.profit').width('24em');*/
            $('.profit').addClass('moneyMessage');
            $('#allMoney, #allDiv').css('font-size','33px');
            window.setTimeout(function(){
                self.changeMoney();
            },400);
        });

    };

    this.forthEvent=function(){
        var self=this;
        var animateObj=$(".swiper-wrapper").children(".swiper-slide").eq(3).children(".mainImg");
        self.commonAnimate(animateObj,'slideFromRight',500,'linear',0,function(){
            //金币第一次滑落
            var dropMiney1 =$(".swiper-wrapper").children(".swiper-slide").eq(3).children(".dropMoney1");
            self.commonAnimate(dropMiney1,'dropMoney',500,'linear',0,function(){
                dropMiney1.css('visibility','hidden');
                $(".singleMoney1").css('visibility','visible');
            });

            //右侧金币滑落
            var dropMiney2 =$(".swiper-wrapper").children(".swiper-slide").eq(3).children(".dropMoney3");
            self.commonAnimate(dropMiney2,'dropMoney',500,'linear',0,function(){
                dropMiney2.css('visibility','hidden');
                $(".singleMoney3").css('visibility','visible');
            });

            //二次金币滑落
            var dropMiney3 =$(".swiper-wrapper").children(".swiper-slide").eq(3).children(".dropMoney2");
            self.commonAnimate(dropMiney3,'dropMoney',500,'linear',0,function(){
                dropMiney3.css('visibility','hidden');
                $(".singleMoney2").css('visibility','visible');

                var messageAnimate= $(".swiper-wrapper").children(".swiper-slide").eq(3).children(".message");
                self.commonAnimate(messageAnimate,'firstMainImg',500,'linear',0);
            });
        });
    };

    this.fifthEvent=function(){
        var self=this;
        var textAnimateObj=$("#messageH");
        self.commonAnimate(textAnimateObj,'rotateyTransform',500,'linear',0,function(){
            var animateObj=$(".swiper-wrapper").children(".swiper-slide").eq(4).children(".mainImg");
            self.commonAnimate(animateObj,'slideFromRight',500,'linear',0,function(){
                var littleboyAnimate=$(".swiper-wrapper").children(".swiper-slide").eq(4).children(".mainImg").children(".littleBoy");
                self.commonAnimate(littleboyAnimate,'firstMessage',500,'linear',0,function(){
                    var messageAnimate= $(".swiper-wrapper").children(".swiper-slide").eq(4).children(".message");
                    self.commonAnimate(messageAnimate,'rotateyTransform',500,'linear',0);
                });
            });
        });
    };

    //改变金钱数值
    this.changeMoney=function(){
        var self=this;
        var runCount=0;
        var changeInterval=window.setInterval(function(){
            runCount++;
            if(runCount<=7){
                var allValue=runCount*10000+self.GetRandomNum(0,9)*1000+self.GetRandomNum(0,9)*100+self.GetRandomNum(0,9)*10+self.GetRandomNum(0,9)*1000;
                var allProfit=runCount*1000+self.GetRandomNum(0,9)*100+self.GetRandomNum(0,9)*10+self.GetRandomNum(0,9)*1000;
                var newProfit=runCount*100+self.GetRandomNum(0,9)*10+self.GetRandomNum(0,9)*1000;
                $('#allMoney').html(allValue);
                $('#allProfit').html(allProfit);
                $('#newProfit').html(newProfit);
            }
            else{
                if(runCount>=11){
                    window.clearInterval(changeInterval);
                  /*  $('.profit').css('background-color', 'transparent');
                    $('.profit').css('marginLeft', '-30vh');
                    $('.profit').css('font-size', '12px');
                    $('.profit').css('top', '15%');
                    $('.profit').width('60vh');
                    $('.profit').css('border-color', 'transparent');*/
                    return;
                }
                if(runCount<=8) {
                    $('#allMoney').html('88888');
                    $('#allProfit').html('8888');
                    $('#newProfit').html('888');
                }
            }

        },100);
    }

    this.GetRandomNum=function (Min,Max)
    {
        var Range = Max - Min;
        var Rand = Math.random();
        return(Min + Math.round(Rand * Range));
    }
}